package com.example.secondhandsysapi.Controller;


import com.example.secondhandsysapi.Entity.NoticeInfo;
import com.example.secondhandsysapi.Service.NoticeInfoService;
import com.example.secondhandsysapi.util.FileUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("notice")
public class NoticeInfoController {
    @Autowired
    private NoticeInfoService noticeInfoService;

    //首页得到随机的九条公告信息
    @GetMapping("/random")
    public List<NoticeInfo> getRandomNoticeInfo(){
        return noticeInfoService.getRandomNoticeInfo();
    }

    @GetMapping("/querybytype")
    public List<NoticeInfo> getInfoByType(String type){
        return noticeInfoService.getInfoByType(type);
    }

    @GetMapping("/querybytheme")
    public List<NoticeInfo> getInfoByStatus(Integer status){
        return noticeInfoService.getInfoByStatus(status);
    }

    @GetMapping("/query")
    public List<NoticeInfo> getInfoByTypeAndTheme(@RequestParam(required = false) String type, @RequestParam(required = false) Integer status){
        List<NoticeInfo> noticeallinfo = null;
        if(type != null && status != null){
            // 公告类型和主题名称都不为空时，根据类型和主题名查公告信息
            noticeallinfo = noticeInfoService.getInfoByTypeAndStatus(type,status);
        } else if(type != null) {
            // 当类型不为空时，根据类型查公告信息
            noticeallinfo = noticeInfoService.getInfoByType(type);
        } else if(status != null) {
            // 当主题名不为空时，根据主题名查公告信息
            noticeallinfo = noticeInfoService.getInfoByStatus(status);
        } else {
            // 当主题名和类型都为空时，返回所有公告信息
            noticeallinfo = noticeInfoService.getAllNoticeInfo();
        }
        return noticeallinfo;
    }

    @PostMapping("/insert")
    public Integer addNoticeInfo(@RequestBody NoticeInfo noticeInfo){
        Integer i = noticeInfoService.addNoticeInfo(noticeInfo);
        return i;
    }

    @PostMapping("/update")
    public Integer updateNoticeInfo(@RequestBody NoticeInfo noticeInfo){
        Integer i = noticeInfoService.updateNoticeInfo(noticeInfo);
        return i;
    }

    @GetMapping("/del/{id}")
    public Integer deleteNoticeById(@PathVariable Integer id){
        return noticeInfoService.deleteNoticeInfo(id);
    }

    //http://localhost:8080/notice/upload
    @PostMapping("/upload")
    public String getImageFileName(MultipartFile file) {
        String oldFileName = file.getOriginalFilename();
        System.out.println(oldFileName);
        String typeName = oldFileName.substring(oldFileName.lastIndexOf("."));
        String filePath = FileUtil.getUpLoadFilePath();
        System.out.println(typeName);
        String newFileName = System.currentTimeMillis() + typeName;

        try {
            FileUtil.uploadFile(file.getBytes(),filePath,newFileName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return newFileName;
        }



    // http://localhost:8080/notice/checknotice
    @GetMapping("/checknotice")
    public List<NoticeInfo> checkNoticeByStatus(@RequestParam(required = false) String notice_type, @RequestParam(required = false) String notice_theme) {
        List<NoticeInfo> result;

        if ((notice_type == null || notice_type.isEmpty()) && (notice_theme == null || notice_theme.isEmpty())) { // 全为空
            result = noticeInfoService.getAllNoticeInfo1();
        } else if (notice_theme == null || notice_theme.isEmpty()) { // notice_type不为空，notice_theme为空
            result = noticeInfoService.getAllNoticeInfoByType(notice_type);
        } else if (notice_type == null || notice_type.isEmpty()) { // notice_type为空，notice_theme不为空
            result = noticeInfoService.getAllNoticeInfoByTheme(notice_theme);
        } else { // notice_type和notice_theme均不为空
            result = noticeInfoService.getAllNoticeInfo2(notice_type, notice_theme);
        }

        return result;
    }


    // http://localhost:8080/notice/page
    @GetMapping("page")
    public PageInfo<NoticeInfo> getNoticeInfo(@RequestParam Integer page, @RequestParam Integer size){
        return noticeInfoService.getNoticeInfo(page,size);
    }

    // http://localhost:8080/notice/updateStatus/
    @PostMapping("updateStatus/{notice_id}")
    public Integer updateStatus(@PathVariable Integer notice_id){
        Integer i= noticeInfoService.updateStatus(notice_id);
        return 1;
    }

    // http://localhost:8080/notice/updateCheck_info
    @PostMapping("updateCheck_info")
    public Integer updateCheckInfo( String check_info,Integer notice_id){
        Integer i =noticeInfoService.updateCheck_Info(check_info,notice_id);
        return i;
    }

    //  获取公告详情页信息
    @GetMapping("showDetails")
    public NoticeInfo getShowNoticeInfo(@RequestParam Integer notice_id){
        NoticeInfo result =  noticeInfoService.getNoticeDetailsInfoById(notice_id);
        return result;
    }

    //首页查询所有文章
    @GetMapping("/queryArticle")
    public List<NoticeInfo> getAllNoticeInfo(){
        return noticeInfoService.getAllNoticeInfo();
    }

}

